package com.yequan.spring.ioc.overview.dependency.lookup;

import com.yequan.spring.ioc.overview.annotation.Super;
import com.yequan.spring.ioc.overview.domain.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Map;

/**
 * 依赖查找实例
 * 1. 通过名称的方式来查找
 * 2. 通过类型的方式来查找
 * 3. 根据bean名称+类型查找
 * 4. 根据java注解查找
 *
 * @author : Administrator
 * @date : 2020/3/31
 */
public class DependencyLookupDemo {

    public static void main(String[] args) {

        /**
         * 配置 xml 配置文件
         * 启动 Spring 应用上下文
         */
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath:/META-INF/dependency-lookup-context.xml");

        //实时查找
        lookupInRealTime(beanFactory);
        //延时查找
        lookupInLazyTime(beanFactory);

        //类型查找:查找单个bean
        lookupByType(beanFactory);
        //类型查找:查找bean集合
        lookupCollectionByType(beanFactory);

        //根据bean名称+类型查找
        lookupByNameAndType(beanFactory);

        //根据java注解查找
        lookupByAnnotation(beanFactory);

    }

    private static void lookupByAnnotation(BeanFactory beanFactory) {

        if (beanFactory instanceof ListableBeanFactory) {
            ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory;
            Map<String, User> map = (Map) listableBeanFactory.getBeansWithAnnotation(Super.class);
            System.out.println("根据java注解查找: " + map);
        }

    }

    /**
     * 根据bean名称+类型查找
     *
     * @param beanFactory
     */
    private static void lookupByNameAndType(BeanFactory beanFactory) {
        User user = beanFactory.getBean("user", User.class);
        System.out.println("根据bean名称+类型查找: " + user);
    }

    /**
     * 类型查找:查找bean集合
     *
     * @param beanFactory
     */
    private static void lookupCollectionByType(BeanFactory beanFactory) {
        if (beanFactory instanceof ListableBeanFactory) {
            Map<String, User> userMap = ((ListableBeanFactory) beanFactory).getBeansOfType(User.class);
            System.out.println("类型查找: 查找集合bean: " + userMap);
        }
    }

    /**
     * 通过类型查找查找单个bean
     *
     * @param beanFactory
     */
    private static void lookupByType(BeanFactory beanFactory) {
        User user = beanFactory.getBean(User.class);
        System.out.println("类型查找: 查找单个bean: " + user);
    }

    /**
     * 延迟查找
     *
     * @param beanFactory
     */
    private static void lookupInLazyTime(BeanFactory beanFactory) {
        ObjectFactory<User> objectFactory = (ObjectFactory<User>) beanFactory.getBean("objectFactory");
        User user = objectFactory.getObject();
        System.out.println("名称查找: 延迟查找 " + user);
    }

    /**
     * 实时查找
     *
     * @param beanFactory
     */
    private static void lookupInRealTime(BeanFactory beanFactory) {
        //实时查找
        User user = (User) beanFactory.getBean("user");
        System.out.println("名称查找: 实时查找 " + user);
    }

}
